﻿using DroidLord.data;
using SharpAdbClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DroidLord.Core
{
    class AdbDeviceManager
    {
        public static DevScreenInfo  getDeviceScreenSize(DeviceData devData)
        {
            DevScreenInfo devScreenInfo = new DevScreenInfo();

            String shellCmd = "wm size";
            string retMsg=AdbManager.executeShell(devData, shellCmd);
            string[] msgs = retMsg.Split(new char[] { '\r', '\n' });
            if (msgs != null)
            {
                foreach (string line in msgs)
                {
                    string myline = line.Replace(" ","").Replace("\t","").Trim();
                    if (myline.StartsWith("Physicalsize:"))
                    {
                       
                    }
                }
            }
            return devScreenInfo;
        }


        public static Boolean isScreenOff(DeviceData deviceData)
        {
            String shellCmd = "dumpsys window policy";
            string retMsg = AdbManager.executeShell(deviceData, shellCmd);
            string[] msgs = retMsg.Split(new char[] { '\r', '\n' });
            if (msgs != null)
            {
                foreach (string line in msgs)
                {
                    string myline = line.Replace(" ", "").Replace("\t", "").Trim();
                    if (myline.Contains("mScreenOnFully=false"))
                    {
                        return true;
                    }
                }
            }
            return false;
        }


      /// <summary>
      /// 获取当前接入的设备列表
      /// </summary>
      /// <returns></returns>
        public static List<DeviceData> getDevices()
        {
            List<DeviceData> emulators = new List<DeviceData>();

            string cmd = AdbManager.adbPath + " devices -l";
            string strs = AdbManager.ExecCmd(cmd);
            string[] str = strs.Split(new char[] { '\r', '\n' });

            for (int i = 0; i < str.Length; i++)
            {
                string mylines = str[i].Trim();
                if (mylines.Contains("device") && mylines.Contains("product:"))
                {
                    string[] myinfos = mylines.Split(new char[] { ' ', '\t' });
                    DeviceData deviceData = new DeviceData();
                    List<String> infoList = new List<string>();
                    foreach (string myline in myinfos)
                    {
                        string templine = myline.Replace(" ", "").Trim();
                        if (templine.Equals(""))
                        {
                            continue;
                        }
                        infoList.Add(templine);
                    }
                    if (infoList.Count == 5)
                    {
                        string sn = infoList[0].Trim();
                        string model = "";
                        string[] modelInfos = infoList[3].Split(new char[] { ':' });
                        if (modelInfos != null && (modelInfos.Length == 2))
                        {
                            model = modelInfos[1].Trim();
                        }
                        deviceData.Serial = sn;
                        
                        deviceData.Model = model;
                        deviceData.State = DeviceState.Online;
                        emulators.Add(deviceData);
                    }
                }
            }

            return emulators;
        }
    }
}
